package Autoregister;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

import javax.swing.JOptionPane;

import org.omg.CORBA.PUBLIC_MEMBER;

import com.sun.org.apache.bcel.internal.generic.NEW;

import jdk.internal.dynalink.beans.StaticClass;

/**
 *
 * @author LIN
 */
public class FirstRegister extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public FirstRegister() {
        initComponents();
        super.setTitle("管理员界面");
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		super.setSize(400, 350);
		super.setLocation((width - 400) / 2, (height - 350) / 2);
    }

    private void initComponents() {
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("自动注册并购买彩票");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	try {
					update();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });
        jButton2.setText("查看中奖结果");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	new FirstRegister().setVisible(false);
            	try {
					new findthewin(getthewinlist(),win).setVisible(true);
				} catch (ClassNotFoundException | SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            	//此找中奖名单
            	
            }
        });
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(119, 119, 119)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(122, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(89, Short.MAX_VALUE))
        );
       
        pack();
    }
    
    public ArrayList<users> getthewinlist() throws SQLException, ClassNotFoundException{
    	findwinnumber();
    	Connection con = null;
        Class.forName(driverName); // 加载MySql的驱动程序
    	con = DriverManager.getConnection(url, userName, password);
    	statement = con.createStatement();
    	String strSql = "select * from usemes;";
    	resultSet=statement.executeQuery(strSql);
    	ArrayList<users> uselist=new ArrayList<users>();
    	while(resultSet.next()){
    		int[] num=new int[7];
    		int id=resultSet.getInt(1);
    		for (int i = 0; i < num.length; i++) {
				num[i]=resultSet.getInt(4+i);
			}
    		Arrays.sort(num);
    		int shot=0;
    		int fail=0;
    		for (int i = 0; i < num.length; i++) {
				if(win[i]==num[i]){
					shot++;
				}else{
					fail++;
				}
				if(fail>3){
					break;
				}
			}
    		if(shot==4){
    			uselist.add(new users(id,num,resultSet.getInt(4),"三等奖"));
    		}
    		if(shot==5){
    			uselist.add(new users(id,num,resultSet.getInt(4),"二等奖"));
    		}
    		if(shot==6){
    			uselist.add(new users(id,num,resultSet.getInt(4),"一等奖"));
    		}
    		if(shot==7){
    			uselist.add(new users(id,num,resultSet.getInt(4),"一等奖"));
    		}
    	}
    	return uselist;
    }
    
    public void update() throws SQLException{
    	
    	String strSql = "insert into usemes(user,pass,times,num1,num2,num3,num4,num5,num6,num7) values(?,?,?,?,?,?,?,?,?,?)";
    	Connection con = null;
        PreparedStatement prepare = null;
        ResultSet rs = null;
        ArrayList<Integer> list= new ArrayList<Integer>();
        try {
            Class.forName(driverName); // 加载MySql的驱动程序
            con = DriverManager.getConnection(url, userName, password);
            con.setAutoCommit(false);
            prepare = con.prepareStatement(strSql);
            Statement statement=con.createStatement();
            statement.executeUpdate("truncate table usemes");//每次都要先清空
            //此处为自动生成用户
            for (long i = 0; i < usernumber; i++) {
            	//int[] num=new int[7];
            	for (int k = 1; k <= 36; k++) {
        			list.add(k);
        		}
            	prepare.setString(1,(i+1)+"aaa");
                prepare.setString(2, "123");
                prepare.setInt(3,new Random().nextInt(10)+1);//设置随机倍数1-10
                for (int j = 0; j < 7; j++) {
					int locat=new Random().nextInt(list.size());
					//num[j]=list.get(locat);//去除已选的号码	
					prepare.setInt(j+4,list.remove(locat));//将随机产生的号码放入数据库
				}
                list.clear();
                prepare.addBatch();
            }
            prepare.executeBatch();
            con.commit();
        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            if (prepare != null)
                try {
                	prepare.close();// 关闭语句*/
                } catch (Exception e) {
                    e.printStackTrace();
                }
            if (con != null) {
                try {
                    con.close();// 关闭连接
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            
        }
        JOptionPane.showMessageDialog(null,"已注册"+usernumber);
    }
    
    private void findwinnumber() throws SQLException {
    	win=new int[7];
        Connection con=null;
        con = DriverManager.getConnection(url, userName, password);
    	String sql = "select * from winmes;";
		statement=con.createStatement();
		resultSet = statement.executeQuery(sql);
		resultSet.next();
		for (int j = 0; j < 7; j++) {
			win[j]=resultSet.getInt(2+j);
		}
		Arrays.sort(win);
		//return win;
	}

	/**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FirstRegister().setVisible(true);
            }
        });
    }
    public class users
    {
    	public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public int[] getNumlist() {
			return numlist;
		}
		public void setNumlist(int[] numlist) {
			this.numlist = numlist;
		}
		public int getTimes() {
			return times;
		}
		public void setTimes(int times) {
			this.times = times;
		}
		public String getGrade() {
			return grade;
		}
		public void setGrade(String grade) {
			this.grade = grade;
		}
		private int id;
    	public users(int id, int[] numlist, int times, String grade) {
			super();
			this.id = id;
			this.numlist = numlist;
			this.times = times;
			this.grade = grade;
		}
		private int[] numlist=new int[7];
    	private int times;
    	private String grade;
    }
    private int[] win;
	private String url = "jdbc:mysql://localhost:3306/message?useUnicode=true&characterEncoding=GBK&useSSL=false";
	private String userName = "root";
	private String password = "552364871";
	private String driverName = "com.mysql.jdbc.Driver";
	private static Connection connection = null;
	private static Statement statement = null;
	private static ResultSet resultSet = null;
	private long usernumber=100000;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
}
